package com.reptile;

import java.io.*;
import java.net.*;

/**
 * @ClassName PostgreSQLSaveFileDemo
 * @Description 爬取多个图片时需要使用到 reptile_jar 文件夹中的jar包
 * @Author ZhangY
 * @Date 2020/1/22 10:36
 * @Version 1.0.0
 */
public class ReptileForPhoto {

    //要爬的图片地址
    public static String URL="https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1091405991,859863778&fm=26&gp=0.jpg";


    public static void main(String[] args) {
        reptileForPhotoForOne(URL);//爬取一张图片


    }

    /**
      * @Description 爬取一张图片
      * @author ZhangYue26
      * @date 2020/8/11
      * @param [urlForOne]
      * @return void
      **/
    public static void reptileForPhotoForOne(String urlForOne){
        try {

            URL url=new URL(urlForOne);

            //连接
            URLConnection connection=url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

            //获取链接资源内容
            InputStream inputStream=connection.getInputStream();

            //要存储的地方
            FileOutputStream outputStream=new FileOutputStream("D:\\22.jpg");

            byte[] bytes=new byte[1024];
            int length = -1;
            while ((length = inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,length);
            }

            outputStream.close();//关闭
            inputStream.close();//关闭

        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }



    

}